package net.ojbk.timeSpace.test.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import net.ojbk.timeSpace.test.beans.Img;
import net.ojbk.timeSpace.test.beans.InfoAxisBlade;
import net.ojbk.timeSpace.test.beans.TimeAxis;
import net.ojbk.timeSpace.test.beans.TimeBlade;
import net.ojbk.timeSpace.test.dao.ImgDao;
import net.ojbk.timeSpace.test.dao.TimeAxisDao;
import net.ojbk.timeSpace.test.dao.TimeBladeDao;
import net.ojbk.timeSpace.test.service.IAxisService;

@Service("axisService")
public class AxisServiceImpl implements IAxisService {
	@Autowired
	@Qualifier("timeAxisDao")
	private TimeAxisDao timeAxisDao;
	
	@Autowired
	@Qualifier("timeBladeDao")
	private TimeBladeDao timeBladeDao;
	
	@Autowired
	@Qualifier("imgDao")
	private ImgDao imgDao;
	
	@Override
	public List<TimeAxis> getAllAxis() {
		// TODO Auto-generated method stub
		return timeAxisDao.selectAllRecord();
	}

	@Override
	public TimeAxis getOneAxis(int axisId) {
		// TODO Auto-generated method stub
		return timeAxisDao.selectOneRecord(axisId);
	}

	@Override
	public List<TimeBlade> getAllBladeByAxisId(int axisId) {
		// TODO Auto-generated method stub
		return timeBladeDao.selectAllRecordByAxisId(axisId);
	}

	@Override
	public void modifyMsgPw(TimeAxis axis, String newPw) {
		// TODO Auto-generated method stub
		axis.setMsgPw(newPw);
		timeAxisDao.updateOneRecord(axis);
	}

	@Override
	public Boolean checkMsgPw(String yourPw, TimeAxis axis) {
		// TODO Auto-generated method stub
		axis = timeAxisDao.selectOneRecord(axis.getAxisId());
		if(axis.getMsgPw().equals(yourPw)) {
			return true;
		}
		else
			return false;
	}

	@Override
	public void addTimeBlade(TimeAxis axis, TimeBlade newBlade) {
		int newId = (int)(1000 + Math.random() * 9500);
		newBlade.setTimeBladeId(newId);
		newBlade.setPushDate(new Date());
		newBlade.setAxisId(axis.getAxisId());
		timeBladeDao.addOneRecord(newBlade);
	}

	@Override
	public List<TimeAxis> getAllAxisByUid(String uid) {
		List<TimeAxis> meTimeAxis = new ArrayList<>();
		for (TimeAxis timeAxis : getAllAxis()) {
			if (timeAxis.getUid().equals(uid)) {
				meTimeAxis.add(timeAxis);
			}
		}
		// 万不得已的方法,之后servletContext存入map集合。
		
		return meTimeAxis;
	}

	@Override
	public List<InfoAxisBlade> getAllInfoAxisBlade(int axisId) {
		List<InfoAxisBlade> list = new ArrayList<>();
		 for (TimeBlade blade : this.getAllBladeByAxisId(axisId)) {
			 InfoAxisBlade t = new InfoAxisBlade();
			 t.setTimeBladeId(blade.getTimeBladeId());
			 t.setTimeBladeInfo(blade.getTimeBladeInfo());
			 t.setTimeBladeTitle(blade.getTimeBladeTitle());
			 t.setPushDate(blade.getPushDate());
			 List<String> sl = new ArrayList<>();
			 for (Img img : imgDao.selectAllRecordByTimeBladeId(blade.getTimeBladeId())) {
				 System.out.println(img.getImgUUID());
				 sl.add(img.getImgUUID());
			 }
			 t.setImgUUIDList(sl);
			 list.add(t);
		 }
		// 这是我写过的 最烂 最烂 最烂 最烂 最烂 最烂 最烂 的代码，最烂  最烂 最烂 最烂 最烂 的架构。
		return list;
	}

}
